package com.szw.dlyk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szw.dlyk.constant.Constant;
import com.szw.dlyk.domain.ClueRemark;
import com.szw.dlyk.mapper.ClueMapper;
import com.szw.dlyk.query.ClueRemarkQuery;
import com.szw.dlyk.service.ClueRemarkService;
import com.szw.dlyk.mapper.ClueRemarkMapper;
import com.szw.dlyk.util.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author asus
* @description 针对表【t_clue_remark(线索跟踪记录表)】的数据库操作Service实现
* @createDate 2024-01-21 18:16:51
*/
@Service
public class ClueRemarkServiceImpl extends ServiceImpl<ClueRemarkMapper, ClueRemark>
    implements ClueRemarkService{
    @Resource
    private ClueRemarkMapper clueRemarkMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveClueRemark(ClueRemarkQuery clueRemarkQuery) {
        ClueRemark clueRemark = new ClueRemark();

        //把ClueRemarkQuery对象里面的属性数据复制到TClueRemark对象里面去(复制要求：两个对象的属性名相同，属性类型要相同，这样才能复制)
        BeanUtils.copyProperties(clueRemarkQuery, clueRemark);

        clueRemark.setCreateTime(new Date()); //创建时间

        //登录人的id
        Integer loginUserId = JwtUtil.parseUserFromJWT(clueRemarkQuery.getToken()).getId();
        clueRemark.setCreateBy(loginUserId); //创建人

        return clueRemarkMapper.insertSelective(clueRemark);
    }

    @Override
    public PageInfo<ClueRemark> getClueRemarkByPage(Integer current, ClueRemarkQuery clueRemarkQuery) {
        // 1.设置PageHelper
        PageHelper.startPage(current, Constant.PAGE_SIZE);
        // 2.查询
        List<ClueRemark> list = clueRemarkMapper.selectClueRemarkByPage(clueRemarkQuery);
        // 3.封装分页数据到PageInfo
        PageInfo<ClueRemark> info = new PageInfo<>(list);
        return info;
    }
}




